package gigapaint;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;

@SuppressWarnings("serial")
public class PaintPanel extends JPanel
{
	private Color pcolor = Color.BLACK, fcolor = Color.BLACK;
	private boolean isDotted = false, isFilled = false,
			polygonPainting = false;
	private int pmode, drawSize = 1;
	private Paintable actPainting;
	private MyPolygon actPolygon;
	private List<Paintable> list = new ArrayList<Paintable>();
	private List<MyPolygon> polygonList = new ArrayList<MyPolygon>();

	public PaintPanel()
	{

		this.addMouseListener(new MouseAdapter()
		{

			@Override
			public void mousePressed(MouseEvent e)
			{

				switch (pmode)
				{
					case Modes.line:
						actPainting = new MyLine(new Point(e.getX(), e.getY()),
								new Point(e.getX(), e.getY()), pcolor, drawSize);
						break;
					case Modes.point:
						actPainting = new MyPoint(
								new Point(e.getX(), e.getY()), pcolor, drawSize);
						list.add(actPainting);
						break;
					case Modes.rect:
						actPainting = new MyRectangle(new Point(e.getX(),
								e.getY()), new Point(e.getX(), e.getY()),
								pcolor, fcolor, drawSize, isFilled);
						break;
					case Modes.circle:
						actPainting = new MyCircle(
								new Point(e.getX(), e.getY()), new Point(
										e.getX(), e.getY()), pcolor, fcolor,
								drawSize, isFilled);
						break;
					case Modes.polygon:

						if (polygonPainting == true)
						{
							if (e.getButton() == MouseEvent.BUTTON3)
							{
								polygonPainting = false;
								polygonList.add(actPolygon);
							}
							else if (e.getButton() == MouseEvent.BUTTON1)
							{
								actPolygon.addPoint(e.getX(), e.getY());
							}
						}
						else
						{
							polygonPainting = true;
							ArrayList<Point> l = new ArrayList<Point>();
							l.add(new Point(e.getX(), e.getY()));
							actPolygon = new MyPolygon(l, pcolor);
						}
						break;
					case Modes.fill:
						for (MyPolygon p : polygonList)
						{
							if (p.contains(e.getPoint()))
							{
								list.addAll(p.fill(fcolor));
								break;
							}
						}

						break;

				}
				isDotted = true;
				repaint();
			}

			@Override
			public void mouseReleased(MouseEvent e)
			{
				if (pmode != Modes.fill && pmode != Modes.polygon)
				{
					actPainting.setEnd(new Point(e.getX(), e.getY()));
					if (!polygonPainting)
					{
						list.add(actPainting);

						isDotted = false;
						repaint();
					}
				}

			}
		});
		this.addMouseMotionListener(new MouseMotionAdapter()
		{

			@Override
			public void mouseDragged(MouseEvent e)
			{
				switch (pmode)
				{
					case Modes.point:
						actPainting = new MyPoint(
								new Point(e.getX(), e.getY()), pcolor, drawSize);
						list.add(actPainting);
						break;
					default:
						if (pmode != Modes.polygon && pmode != Modes.fill)
							actPainting.setEnd(new Point(e.getX(), e.getY()));
						break;
				}
				repaint();

			}
		});
	}

	public void paintComponent(Graphics g) // Verantwortlich fuer Repaint
	{
		super.paintComponent(g);
		Graphics2D g2D = (Graphics2D) g;

		for (Paintable p : list)
			p.painting(g2D, false);

		for (MyPolygon p : polygonList)
		{
			p.painting(g2D, false);
		}

		if (isDotted == true) // Für gestricheltes Zeichnen
		{
			if (polygonPainting)
				actPolygon.painting(g2D, true);
			else if(actPainting != null)
				actPainting.painting(g2D, true);
		}

	}

	public List<Paintable> getList()
	{
		return list;
	}

	public void setList(List<Paintable> list)
	{
		this.list = list;
		repaint();
	}

	public Color getPcolor()
	{
		return pcolor;
	}

	public void setPcolor(Color pcolor)
	{
		this.pcolor = pcolor;
	}

	public Color getFcolor()
	{
		return fcolor;
	}

	public void setFcolor(Color fcolor)
	{
		this.fcolor = fcolor;
	}

	public boolean isFillMode()
	{
		return isFilled;
	}

	public void setFillMode(boolean fillMode)
	{
		this.isFilled = fillMode;
	}

	public int getPmode()
	{
		return pmode;
	}

	public void setPmode(int pmode)
	{
		this.pmode = pmode;
	}

	public void setDrawSize(int drawSize)
	{
		this.drawSize = drawSize;
	}

	public void clearList()
	{
		list.clear();
		repaint();

	}

}
